package com.zlzxm.shoesmarket.viewmodel;

import androidx.lifecycle.MutableLiveData;

import com.zlzxm.shoesmarket.net.RequestTip;
import com.zlzxm.shoesmarket.net.repository.UserRepository;
import com.zlzxm.shoesmarket.net.request.RqRegister;
import com.zlzxm.shoesmarket.net.respone.BaseResponse;
import com.zlzxm.shoesmarket.util.RxAndroidHelp;
import com.zlzxm.shoesmarket.util.ZStringHelp;

import io.reactivex.rxjava3.functions.Consumer;

/**
 * Created by zlz
 * on  5/11/21
 */
public class VMSignUp extends BaseViewModel{

    UserRepository userRepository = new UserRepository();

    public MutableLiveData<Boolean> liveIsBtnEnable = new MutableLiveData<>(false);

    public MutableLiveData<String> livePhone = new MutableLiveData<>();

    public MutableLiveData<String> livePwd = new MutableLiveData<>();

    public MutableLiveData<String> liveCode = new MutableLiveData<>();


    public  void updateInput(String phone,String code,String pwd){

        livePhone.setValue(phone);
        livePwd.setValue(pwd);
        liveCode.setValue(code);

        liveIsBtnEnable.setValue( !ZStringHelp.isEmpty(phone, code,pwd) );



    }

    public void requestSignUp(){

        RqRegister rqRegister = new RqRegister(liveCode.getValue(), livePwd.getValue(), livePhone.getValue(),0);

        userRepository.register(rqRegister)
                .compose(RxAndroidHelp.turn())
                .subscribe(new Consumer<BaseResponse<String>>() {
                    @Override
                    public void accept(BaseResponse<String> response) throws Throwable {

                        if (response.getCode() == 0){

                            liveSucceedToast.setValue("注册成功");
                        }else {

                            liveErrorToast.setValue(response.getMsg());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Throwable {
                        liveErrorToast.setValue(RequestTip.REQUEST_ERROR);
                    }
                });


    }

}
